package com.cuit.freshmanMS.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.freshmanMS.domain.Class_;
import com.cuit.freshmanMS.domain.Student;
import com.cuit.freshmanMS.mapper.ClassMapper;
import com.cuit.freshmanMS.mapper.StudentMapper;
import com.cuit.freshmanMS.query.ClassQuery;
import com.cuit.freshmanMS.service.ClassService;
import com.cuit.freshmanMS.util.StringUtil;
import com.cuit.freshmanMS.util.result.CommonResult;
import com.cuit.freshmanMS.util.result.PageResult;
import com.cuit.freshmanMS.util.result.ResultCode;
import com.cuit.freshmanMS.util.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Class_> loadAll() {
        return classMapper.loadAll();
    }

    @Override
    public CommonResult deleteAcaByCno(Integer cno) {
        if (cno == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        boolean exists = classMapper.exists(new QueryWrapper<>(new Class_(cno,null, null,null,null)));
        if (!exists){
            return ResultUtil.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
        }

        Student temp = new Student();
        temp.setCno(cno);
        boolean studentExists = studentMapper.exists(new QueryWrapper<>(temp));
        if (studentExists){
            return ResultUtil.fail(ResultCode.EXIST_STUDENT_IN_CLASS);
        }

        classMapper.deleteById(cno);
        return ResultUtil.success();
    }

    @Override
    public CommonResult batchDelete(List<Integer> cnos) {
        Integer count = classMapper.deleteBatchIds(cnos);
        return ResultUtil.success(count);
    }

    @Override
    public CommonResult addClass(Class_ class_) {
        if (class_.getAno() == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtil.isNull(class_.getTeacherName(), class_.getTeacherTel(),class_.getCname())) {
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        classMapper.insert(class_);
        return ResultUtil.success();
    }

    @Override
    public CommonResult updateClass(Class_ class_) {
        // 错误检测
        if (class_.getCno() == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        if (class_.getAno() == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtil.isNull(class_.getTeacherName(), class_.getTeacherTel(),class_.getCname())) {
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        classMapper.updateById(class_);
        return ResultUtil.success();
    }

    @Override
    public PageResult<Class_> selectAll(ClassQuery classQuery) {
        if (classQuery.getCurrentPage() == null || classQuery.getPageSize() == null) {
            return new PageResult<>();
        }
        if (classQuery.getCurrentPage() <= 0 || classQuery.getPageSize() <= 0) {
            return new PageResult<>();
        }

        QueryWrapper<Class_> queryWrapper = new QueryWrapper<>(new Class_())
                .like("cname", classQuery.getCnameK())
                .like("teacher_name", classQuery.getTeacherNameK());
        if (classQuery.getAnoK() != null) {
            queryWrapper.eq("ano", classQuery.getAnoK());
        }
        Page<Class_> resultPage = classMapper.selectPage(
                new Page<>(classQuery.getCurrentPage(), classQuery.getPageSize()), queryWrapper);

        return new PageResult<>(resultPage);

    }
}
